<?php
/**
 * Login.php
 * CopyRight (C) 2019 http://www.xiaomoren.com
 * Author  Showkw <showkw@163.com>
 * Date   2019/10/11 11:33
 */

namespace app\controller;


use app\BaseController;
use app\repository\UserRepository;
use think\facade\Session;
use xmr\helper\BaseFunction;
use xmr\response\Code;
use xmr\util\TokenUtil;

/**
 * Class Login
 *
 * @package app\controller
 * @author Showkw <showkw@163.com>
 * @date   2019/10/11 11:33
 */
class Login extends BaseController
{

	protected $middleware = [];

	/**
	 * @var UserRepository
	*/
	protected $userRepository;

	protected function initialize()
	{
		parent::initialize(); // TODO: Change the autogenerated stub
		$this->userRepository = new UserRepository();
	}

	/**
	 * @author Showkw <showkw@163.com>
	 **/
	public function normalSignIn( $mobile, $password )
	{
		$user = $this->userRepository->getUserByMobile($mobile);
		//账号不存在
		if( !$user ){
			return Code::send( Code::NOT_FOUND_USER);
		}
		//密码不正确
		if( !BaseFunction::hash_check($password,$user->password)){
            return Code::send( Code::USER_PASSWORD_ERROR);
        }
        //TODO 生产令牌
		$token = TokenUtil::makeTokenString($user->mobile);
		Session::set('USER_TOKEN',$token);
	}
}
